package com.chenyi.framework.web.service;

import com.chenyi.common.config.ChenYiConfig;
import com.chenyi.common.constant.Constants;
import com.chenyi.common.core.domain.entity.SysShop;
import com.chenyi.common.core.domain.model.LoginShop;
import com.chenyi.common.core.redis.RedisCache;
import com.chenyi.common.enums.UserStatus;
import com.chenyi.common.exception.BadRequestException;
import com.chenyi.common.exception.MessageException;
import com.chenyi.common.utils.*;
import com.chenyi.framework.manager.AsyncManager;
import com.chenyi.framework.manager.factory.AsyncFactory;
import com.chenyi.shop.service.ISysShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ShopLoginService {


    @Autowired
    private ShopTokenService tokenService;

    @Autowired
    private ISysShopService shopService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public String login(String username,String password, String code, String uuid) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);

        SysShop sysShop = shopService.selectShopByUserName(username);
        if (sysShop == null){
            throw new MessageException("用户不存在");
        }
        if (!ShopUtils.matchesPassword(password,sysShop.getPassword())){
            AsyncManager.me().execute(AsyncFactory.recordShopLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"),null));
            throw new MessageException(MessageUtils.message("user.password.not.match"));
        }
        if (!code.equalsIgnoreCase(captcha))
        {
            AsyncManager.me().execute(AsyncFactory.recordAdminLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"),null));
            throw new MessageException(MessageUtils.message("user.jcaptcha.error"));
        }
        if (sysShop.getStatus().equals(UserStatus.DISABLE.getCode())){
            throw new BadRequestException(MessageUtils.message("user.blocked"));
        }
        LoginShop loginShop = new LoginShop(sysShop);

        //清理上次Token
        if (StringUtils.isNotEmpty(loginShop.getShop().getToken()) && ChenYiConfig.getUserSsoLogin()){
            tokenService.delLoginShop(loginShop.getShop().getToken());
            //其它地方登录记录
            AsyncManager.me().execute(AsyncFactory.recordShopLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.otherlogout"),loginShop.getShop().getToken()));
        }

        String token = tokenService.createToken(loginShop);
        //登录成功记录
        AsyncManager.me().execute(AsyncFactory.recordShopLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.login.success"),loginShop.getToken()));

        loginShop.getShop().setToken(loginShop.getToken());
        loginShop.getShop().setLoginDate(new Date(loginShop.getLoginTime()));
        loginShop.getShop().setLoginIp(loginShop.getIpaddr());
        loginShop.getShop().setUpdateTime(DateUtils.getNowDate());
        shopService.updateSysShop(loginShop.getShop());
        return token;
    }
}
